package com.fengjing.launcher.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v17.leanback.widget.VerticalGridView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fengjing.launcher.R;
import com.fengjing.launcher.common.MyAdapter;
import com.fengjing.launcher.model.FeiChangYingYuan;
import com.fengjing.launcher.parse.FeiChangYingYuanParser;
import com.fengjing.launcher.widget.MenuScrollView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/9/19.
 */

public class CategoryActivity extends BaseActivity implements MenuScrollView.OnItemSelectListener {
    private static final String TAG = "CategoryActivity";
    int pos = 0;

    private VerticalGridView gridView;
    private MyAdapter myAdapter;

    private MenuScrollView scrollView;

    List<FeiChangYingYuan> feiChangYingYuanList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);

        gridView = (VerticalGridView) findViewById(R.id.grid_view);
        gridView.setNumColumns(4);

        myAdapter = new MyAdapter();
        myAdapter.setNumColumns(4);
        myAdapter.setChildMarginTop(200);
        myAdapter.setChildMarginBottom(200);
        gridView.setAdapter(myAdapter);

        scrollView = (MenuScrollView) findViewById(R.id.scroll_layout);
        scrollView.setOnItemSelectListener(this);

        try {
            FeiChangYingYuanParser.getFenLeiList(feiChangYingYuanList);

            setTitleList(feiChangYingYuanList);
        }catch (Exception e){
            Toast.makeText(this, "数据获取失败", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    public void onSelect(MenuScrollView view, int p) {

        FeiChangYingYuan feiChangYingYuan = feiChangYingYuanList.get(p);

        if (feiChangYingYuan.getFilmList() != null) {
            myAdapter.setFilmModels(feiChangYingYuan.getFilmList());
            myAdapter.notifyDataSetChanged();
            return;
        }
        new MyThread(p).start();
    }

    class MyThread extends Thread {
        int p;

        public MyThread(int p) {
            this.p = p;
        }

        @Override
        public void run() {

            FeiChangYingYuanParser.getFenLeiXiangQing(feiChangYingYuanList.get(p));
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (scrollView.getSelectPosition() == p) {
                        myAdapter.setFilmModels(feiChangYingYuanList.get(p).getFilmList());
                        myAdapter.notifyDataSetChanged();
                    }
                }
            });
        }
    }

    public void setMenuLayoutHeight(int num) {
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.menu_layout);
        linearLayout.setMinimumHeight(1080 + 128 * (num));
    }

    public void setTitleList(List<FeiChangYingYuan> titleList) {
        ViewGroup viewGroup = (ViewGroup) findViewById(R.id.menu_layout);

        for (int i = 0; i < titleList.size(); i++) {
            viewGroup.addView(getTextView(titleList.get(i).getFenLeiAliasName(), i));
            if (i == 0) {
                onSelect((MenuScrollView) findViewById(R.id.scroll_layout), i);
            }
        }
        setMenuLayoutHeight(titleList.size());
    }

    public TextView getTextView(String name, int pos) {
        TextView textView = new TextView(this);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 28);
        textView.setGravity(Gravity.CENTER);
        textView.setText(name);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(215, 128);

        if (pos == 0) {
            layoutParams.topMargin = 284;
        }

        textView.setLayoutParams(layoutParams);

        return textView;
    }
}
